define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area", ["100%", "100%"]); //添加
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'buchang/baoan/index' + location.search,
                    add_url: 'buchang/baoan/add',
                    // edit_url: 'buchang/baoan/edit',
                    // del_url: 'buchang/baoan/del',
                    multi_url: 'buchang/baoan/multi',
                    import_url: 'buchang/baoan/import',
                    table: 'baoan',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'gs_type', title: __('Gs_type'),operate: false},
                        {field: 'baoan_id', title: __('Baoan_id'), operate: 'LIKE'},
                        {
                            field: 'baoan_time',
                            title: __('Baoan_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'chuxian_time',
                            title: __('Chuxian_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'phone', title: __('Phone'),operate: false},
                        {field: 'tong_number', title: __('Tong_number'), operate: 'LIKE'},
                        {field: 'car_num', title: __('Car_num'), operate: 'LIKE'},
                        {field: 'car_vin', title: __('Car_vin'), operate: 'LIKE'},
                        {field: 'learned_name', title: __('Learned_name'), operate: 'LIKE'},
                        {
                            field: 'zuzhi.name',
                            title: __('归属部门'),
                            searchList: $.getJSON('lists/one/select_zuzhi'),
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'yw_belonger', title: __('Yw_belonger'), operate: 'LIKE'},
                        {
                            field: 'yw_hetong_time',
                            title: __('Yw_hetong_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime,datetimeFormat: "YYYY-MM-DD"
                        },
                        {
                            field: 'plan_endtime',
                            title: __('Plan_endtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime,datetimeFormat: "YYYY-MM-DD"
                        },
                        {field: 'chakan_ren', title: __('Chakan_ren'), operate: 'LIKE'},
                        {field: 'yw_enter_peo', title: __('录入人'), operate: 'LIKE'},
                        {field: 'buchang_money', title: __('Buchang_money'), operate: false},
                        {field: 'baoan_status', title: __('Baoan_status'),
                            searchList: {
                                0: __('未结案'),
                                1: __('已结案'),
                            },formatter: Table.api.formatter.label
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'details',
                                    text: '浏览',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: '',
                                    url: 'buchang/baoan/details',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'details',
                                    text: '调度',
                                    classname: 'btn btn-success btn-xs btn-dialog',
                                    icon: '',
                                    url: 'buchang/diaodu/diaodu',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        fuwudan: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'buchang/baoan/fuwudan' + location.search,
                    table: 'list_one',
                }
            });
            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                // console.log(data);
                //这里我们手动设置底部的值
                $("#all_main_free").text(data.all_main_free);
                $("#all_main_sum").text(data.all_main_sum);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormTemplate: 'list_one',
                search: false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {
                            field: 'status',
                            title: __('状态'),
                            operate: false,
                            formatter: function (value, row, index) {
                                // console.log('index',row)
                                var html = '';
                                if (!row.tong_number) {
                                    html = "<p style=\"color:#32f800;font-weight: 700;border: 1px solid #32f800;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                        "                        暂存</p>";
                                } else {
                                    if (row.status == 6) {
                                        html = "<p style=\"color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        退统</p>";
                                    } else if (row.status == 7) {
                                        html = "<p style=\"color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        作废</p>";
                                    } else {
                                        html = " <p style=\"color:#000;font-weight: 700;border: 1px solid #000;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        正常</p>";
                                    }
                                }
                                return html;
                            }
                        },
                        {
                            field: 'tong_storage',
                            title: __('暂存单号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'tong_number',
                            title: __('服务单号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'zuzhi.name',
                            title: __('组织'),
                            searchList: $.getJSON('lists/one/select_zuzhi'),
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'yw_belonger', title: __('Yw_belonger'), operate: 'LIKE'},
                        {
                            field: 'car_num',
                            title: __('车牌号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'car_vin',
                            title: __('车架号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'car_engine',
                            title: __('发动机号'),
                            visible: false,
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'learned_name',
                            title: __('被服务人'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'yw_join_time',
                            title: __('服务日期'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'yw_hetong_time',
                            title: __('初审日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'yw_shenhe_time',
                            title: __('审核日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'plan_strattime',
                            title: __('服务起期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'plan_endtime',
                            title: __('服务止期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'chexing',
                            title: __('车型'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'main_free',
                            title: __('应缴服务费'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'yw_enter_peo',
                            title: __('录入人'),
                            operate: Config.sxstatus,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'yw_hetongpeo',
                            title: __('审核人'),
                            operate: Config.sxstatus,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'sign',
                            title: __('签字状态'),
                            operate: false,
                            visible: Config.status,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value == 1) {
                                    var html = "已签字";
                                    return html;
                                } else {
                                    var html = "未签字";
                                    return html;
                                }
                            }
                        },
                        {
                            field: 'status',
                            title: __('状态'),
                            searchList: {
                                0: __('暂存'),
                                1: __('待审核'),
                                2: __('待市级审核'),
                                3: __('审核通过待缴费'),
                                4: __('待修改'),
                                5: __('已出单'),
                                6: __('已退统'),
                                7: __('已过期'),
                            },
                            custom: {
                                0: 'success',
                                1: 'default',
                                2: 'warning',
                                3: 'info',
                                4: 'danger',
                                5: 'primary',
                                6: 'huang',
                                7: 'zi'
                            },
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'car_factory_num',
                            title: __('厂牌型号'),
                            visible: false,
                            operate: Config.sxstatus,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // operate-edit编辑  perate-del删除
                                //判断什么时候显示什么时候不显示
                                // if (row.status === 1) {
                                //     $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                //     // $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                // }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [
                                {
                                    name: 'details',
                                    text: '浏览',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: '',
                                    url: 'buchang/baoan/onedetails',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        baoan: function () {
            $(".btn-add").data("area", ["100%", "100%"]); //添加
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'buchang/baoan/baoan' + location.search,
                    table: 'baoan',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#buchang_money").text(data.buchang_money);
                $("#weijue_money").text(data.weijue_money);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                searchFormTemplate: 'baoan',
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'gs_type', title: __('Gs_type'),operate: false},
                        {field: 'baoan_id', title: __('Baoan_id'), operate: 'LIKE'},
                        {
                            field: 'baoan_time',
                            title: __('Baoan_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'chuxian_time',
                            title: __('Chuxian_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'phone', title: __('Phone'),operate: false},
                        {field: 'tong_number', title: __('Tong_number'), operate: 'LIKE'},
                        {field: 'car_num', title: __('Car_num'), operate: 'LIKE'},
                        {field: 'car_vin', title: __('Car_vin'), operate: 'LIKE'},
                        {field: 'learned_name', title: __('Learned_name'), operate: 'LIKE'},
                        {
                            field: 'yw_hetong_time',
                            title: __('Yw_hetong_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime,datetimeFormat: "YYYY-MM-DD"
                        },
                        {
                            field: 'plan_endtime',
                            title: __('Plan_endtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime,datetimeFormat: "YYYY-MM-DD"
                        },
                        {field: 'chakan_ren', title: __('Chakan_ren'), operate: 'LIKE'},
                        {field: 'buchang_money', title: __('Buchang_money'), operate: false},
                        {field: 'baoan_status', title: __('Baoan_status'),
                            searchList: {
                                0: __('未结案'),
                                1: __('已结案'),
                            },formatter: Table.api.formatter.label
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'details',
                                    text: '浏览',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: '',
                                    url: 'buchang/baoan/details',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
